package com.dckeji.shiplock.webapp.conf;

import com.alibaba.druid.support.http.StatViewServlet;
import com.dckeji.shiplock.framework.constants.WebConstants;
import com.dckeji.shiplock.webapp.filter.LoginFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/7/28
 * Time: 18:35
 * 初始化web.xml
 */

public class WebAppInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{WebConstants.Filter.FilterMapping.Root};
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {


        //*****************************Filter********************************
        FilterRegistration.Dynamic enCodingFilter = servletContext.addFilter(WebConstants.Filter.FIlterName.SetCharacterEncoding, CharacterEncodingFilter.class);
        enCodingFilter.addMappingForServletNames(null, true, WebConstants.Filter.FIlterName.SetCharacterEncoding);
        enCodingFilter.addMappingForUrlPatterns(null, true, WebConstants.Filter.FilterMapping.dox);

//        FilterRegistration.Dynamic loginFilter = servletContext.addFilter(WebConstants.Filter.FIlterName.LoginFilter, LoginFilter.class);
//        loginFilter.addMappingForServletNames(null, true, WebConstants.Filter.FIlterName.LoginFilter);
//        loginFilter.addMappingForUrlPatterns(null, true, WebConstants.Filter.FilterMapping.Manager);
//        loginFilter.addMappingForUrlPatterns(null, true, WebConstants.Filter.FilterMapping.RESTManager);

//        FilterRegistration.Dynamic restloginFilter = servletContext.addFilter(WebConstants.Filter.FIlterName.RESTLoginFilter, LoginFilter.class);
//        restloginFilter.addMappingForServletNames(null, true, WebConstants.Filter.FIlterName.RESTLoginFilter);
//        restloginFilter.addMappingForUrlPatterns(null, true, WebConstants.Filter.FilterMapping.RESTManager);

//
//        FilterRegistration.Dynamic CORSFilter = servletContext.addFilter(WebConstants.Filter.FIlterName.CORSFilter, CORSFilter.class);
//        gzipFilter.addMappingForServletNames(null, true, WebConstants.Filter.FIlterName.CORSFilter);
//        gzipFilter.addMappingForUrlPatterns(null, true, WebConstants.Filter.FilterMapping.Root);

        //**************************Servlet********************************
        ServletRegistration.Dynamic druidServlet = servletContext.addServlet(WebConstants.Servlet.ServletName.DruidStatView, StatViewServlet.class);
        druidServlet.addMapping(WebConstants.Servlet.ServletMapping.DruidStatView);
        druidServlet.setInitParameter(WebConstants.Servlet.InitParam.ResetEnable, WebConstants.Servlet.StrTrue);
//        druidServlet.setInitParameter(WebConstants.Servlet.InitParam.LoginPassword, WebConstants.Servlet.UserName);
//        druidServlet.setInitParameter(WebConstants.Servlet.InitParam.LoginUsername, WebConstants.Servlet.Password);

        //*************************Listener******************************
        super.onStartup(servletContext);
    }
}
